<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>所有公告</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/blogger.css">
    <link rel="stylesheet" href="./css/container.css">
    <!-- <link rel="stylesheet" href="./css/blog_content.css"> -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <!-- <link rel="stylesheet" href="editor.md/css/editormd.min.css" /> -->
</head>

<body onload="getUserInfo()">

    <iframe src="i_nav.html" style="height: 45px; width: 100%;" frameborder="0"></iframe>


    <div class="container">
        <iframe src="i_blogger.html" style="height: 534px; width: 274px;" frameborder="0"></iframe>
        <div class="blogs" style="overflow: auto;">
            <div>
                <h2>公告</h2>
                <a href="home.html">
                    <h4>[查看推荐]</h4>
                </a>
                <br>
                <ul id="announcementList"></ul>
            </div>
        </div>
    </div>

    <script>
        //信息获取
        function getUserInfo() {
            //登录验证
            if (localStorage.getItem('token')) {


                const announcementList = document.querySelector('#announcementList')
                axios('http://47.98.20.202:8000/announcement/getAnnouncementList/')
                    .then(res => {
                        const data = res.data.data;
                        // console.log("公告:",data)

                        announcementList.innerHTML = data
                            .sort((a, b) => new Date(b.a_create_time) - new Date(a.a_create_time))
                            .map(item =>
                                `<a href="announcement.html?id=${item.id}">
                                    <li>
                                        <div style="float:left;"><b>【通知】</b></div>
                                        <div style="float:left;">${item.a_content}</div>
                                        <div style="float:right;">[${formatTime(item.a_create_time)}]</div>
                                    </li>
                                </a>                  
                    `).join('')
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });



            }
            else {
                window.location.href = "login.html";
            }
        }


        //时间戳转换为时间
        function formatTime(time) {
            const date = new Date(time * 1000)
            const Y = date.getFullYear()
            const M = date.getMonth() + 1
            const D = date.getDate()
            const h = date.getHours()
            const m = date.getMinutes()
            return `${Y}-${M}-${D} ${h}:${m}`
        }


    </script>
</body>

</html>